<?php 
/**
 * @auth yangjie
 */
class FileEncrtyptComponent extends Object{
	
	var $dir_confirm = null;
	var $dir = null;
	var $app_path = null;
	function __construct(){
		$this->Encrypt = new EncryptComponent();
		$this->prop = new PropComponent();
		$this->dir_confirm = $this->prop->getValue('config.file.dir_confirm');
		$this->dir = $this->prop->getValue('config.file.dir');
		$this->app_path = ROOT . DS . 'app';
	}
	
	
	function upload($file,$path,$filename){
		$this->_creatDir($path);
		move_uploaded_file($file, $this->app_path .'/'. $path . '/' . $filename) or die('move_uploaded_file 失敗しました');
		return true;
	}
	
	function saveConfirmFile($form,$field){
		$file = $form[$field];	
		$file_name = date('YmdHis_').$this->randam('4');
		$arr=explode('.',$file['name']);
		$file_name .= '.'.$arr[count($arr)-1];
		/*switch (substr($file['name'],-3)){
			case 'application/pdf':
				$file_name .= '.pdf';
				break;
			case 'application/vnd.ms-powerpoint':
				$file_name .= '.ppt';
				break;
			case 'application/vnd.ms-excel':
				$file_name .= '.xls';
				break;
			case 'application/vnd.ms-word':
				$file_name .= '.doc';
				break;
			case 'image/jpeg':
				$file_name .= '.jpg';
				break;
			case 'image/gif':
				$file_name .= '.gif';
				break;
		}*/

		$result = $this->upload($file['tmp_name'],$this->dir_confirm,$file_name);
		if($result)
		{
			return $file_name;
		}
		else{
			return null;
		}
	}
	
	function saveFile($path = '',$file_name){
		$read_file = $this->app_path . DS . $this->dir_confirm. DS . $file_name;
		if(empty($path)){
			$this->_creatDir($this->dir);
			$write_file = $this->app_path . DS .$this->dir. DS . $file_name;
		}
		else{
			$this->_creatDir($this->dir.'/'.$path);
			$write_file = $this->app_path . DS .$this->dir. DS . $path . DS . $file_name;
		}
//		$write_file = $this->app_path . $this->dir. DS . $file_name;
		$rh = fopen($read_file, 'rb');
		$wh = fopen($write_file, 'wb');

		// -- 存放文件中的数据
		while (!feof($rh)) {
			$stored = fread($rh, 1024);
			$this->Encrypt->encode($stored);
			$stored .= chr(10);
			fwrite($wh, $stored);
		}
		fclose ($rh);
		fclose ($wh);
		unlink($read_file);
	}
	
	function downloadFile($path,$file_name,$flg){
		$file_path = $this->app_path . DS . $path . DS . $file_name;
		$fileLineArray = file($file_path);

//		switch (substr($file_name,-3)){
//			case 'xls':
//				header("Content-type:application/vnd.ms-excel");
//				break;
//			case 'pdf':
//				header("Content-type:application/vnd.ms-pdf");
//				break;
//			case 'doc':
//				header("Content-type:application/vnd.ms-word");
//				break;
//			case 'ppt':
//				header("Content-type:application/vnd.ms-powerpoint");
//				break;
//			case 'jpg':
//				header("Content-Type: text/plain"); 
//				break;
//			case 'gif':
//				header("Content-Type: text/plain"); 
//				break;
//		}
		
		header("Content-Type: application/octet-stream");
		
		if($flg==1){
			$file_name='k-resume'.$file_name;
		}elseif($flg==2){
			$file_name='k-careersheet'.$file_name;
		}else{
			$file_name='k-other'.$file_name;
		}
		header("Content-Disposition:attachment;filename=$file_name");
		
		foreach($fileLineArray as $fileLine) {
			$this->Encrypt->decode($fileLine);
			echo $fileLine;
		}
	}
	
	function _creatDir($fileDir) {
		$dirs = split("/", $fileDir);
		$path =  $this->app_path ;
		foreach ($dirs as $dir) {
			$path .= DS . $dir;
			if (!file_exists($path)) {
				@ mkdir($path, 0777);
			}
		}
	}
	
	function randam($len){
		$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; 
	    mt_srand((double)microtime()*1000000*getmypid()); // seed the random number generater (must be done)
	
	    $key='';
	
	    while(strlen($key)<$len)
	        $key .= substr($chars,(mt_rand()%strlen($chars)),1);
	
	    return $key;
	}
}
?>